package com.crrcdt.vue_project.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.crrcdt.vue_project.entity.Permission;
import com.crrcdt.vue_project.entity.PermissionApi;
import com.crrcdt.vue_project.result.Result;
import com.crrcdt.vue_project.result.ResultFactory;
import com.crrcdt.vue_project.service.PermissionApiService;
import com.crrcdt.vue_project.service.PermissionService;
import com.crrcdt.vue_project.util.TreeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @author lj on 2020/10/22.
 * @version 1.0
 */
@RestController
@RequestMapping(value = "/api/private/v1")
@Slf4j
public class RightsController {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private PermissionApiService permissionApiService;
    @GetMapping("/menus")
    public Result getMenus(){
        final List<Permission> permissions = permissionService.list(new QueryWrapper<Permission>().ne("ps_level", "2"));
//        System.out.println(permissions);
        // 构建node列表
        if(permissions==null){
            return ResultFactory.buildFailResult("获取菜单数据失败！");
        }
        // 查询path和order
        permissions.stream().forEach(entity->{
            final Integer psId = entity.getPsId();
            final List<PermissionApi> permissionApis = permissionApiService.list(new QueryWrapper<PermissionApi>().eq("ps_id", psId));
            Assert.notEmpty(permissionApis,"未查到相关的菜单数据");
            entity.setPath(permissionApis.get(0).getPsApiPath());
            entity.setOrder(permissionApis.get(0).getPsApiOrder());
        });
        List<Tree<String>> treeList = TreeUtils.getTrees(permissions);
        System.out.println(treeList);
        return ResultFactory.buildSuccessResult(treeList,"获取菜单数据成功！");
    }


    @GetMapping("/rights/{type}")
    public Result getRights(@PathVariable("type") String type){
        final List<Permission> permissions = permissionService.list(null);
        permissions.stream().forEach(entity->{
            final List<PermissionApi> permissionApis = permissionApiService.list(new QueryWrapper<PermissionApi>().eq("ps_id", entity.getPsId()));
            Assert.notEmpty(permissionApis,"permissionApis不能为空！");
            entity.setPath(permissionApis.get(0).getPsApiPath());
        });
        // 构建node列表
        if(permissions==null){
            return ResultFactory.buildFailResult("获取所有权限数据失败！");
        }
        if(type.equals("list")){
            return ResultFactory.buildSuccessResult(permissions,"获取所有权限数据失败！");
        }else {
            List<Tree<String>> treeList = TreeUtils.getTrees(permissions);
            System.out.println(treeList);
            return ResultFactory.buildSuccessResult(treeList,"获取菜单数据成功！");
        }

    }
}
